package com.kqb.sgai.controller;

import com.kqb.sgai.advisor.MyLoggerAdvisor;
import jakarta.annotation.Resource;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.SimpleLoggerAdvisor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

import static org.springframework.ai.chat.client.advisor.AbstractChatMemoryAdvisor.CHAT_MEMORY_CONVERSATION_ID_KEY;
import static org.springframework.ai.chat.client.advisor.AbstractChatMemoryAdvisor.CHAT_MEMORY_RETRIEVE_SIZE_KEY;

/**
 * @author 杉果树皮
 */
@RestController
@RequestMapping("/ai")
public class ChatController {

    @Resource
    private ChatClient dashScopeClient;

    @GetMapping("/chat")
    public Flux<String> chat(@RequestParam("message") String message, @RequestParam("chatId") String chatId) {
        return dashScopeClient
                .prompt()
                .system("你是一个医疗问诊助手，你会根据用户的问题，提供相关的医疗咨询。拒绝回答非医疗相关的问题。")
                .user(message)
                .advisors(spec -> spec.param(CHAT_MEMORY_CONVERSATION_ID_KEY, chatId)
                        .param(CHAT_MEMORY_RETRIEVE_SIZE_KEY, 10)
                )
                .stream()
                .content();
    }

}
